<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="Erhqe">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Erhqe">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/"/>





  <title>Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/16/RedBlackTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/16/RedBlackTree/" itemprop="url">RedBlackTree</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-16T11:06:16+08:00">
                2019-10-16
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/16/RedBlackTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/16/RedBlackTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/14/AVLTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/14/AVLTree/" itemprop="url">AVLTree</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-14T15:07:44+08:00">
                2019-10-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/14/AVLTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/14/AVLTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="AVL树"><a href="#AVL树" class="headerlink" title="AVL树"></a>AVL树</h3><blockquote>
<p><strong>平衡因子 (Balance Factor) 某节点的左右树的高度差</strong></p>
<p><strong>每页节点的平衡因子只可能是,1,0,-1(绝对值 &lt;= 1,如果超过1 称之为 失衡)</strong></p>
<p><strong>搜索,添加,删除的时间复杂度是O(logn)</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071252.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071451.png" alt=""></p>
<h3 id="LL-右旋转-单旋"><a href="#LL-右旋转-单旋" class="headerlink" title="LL - 右旋转 (单旋)"></a>LL - 右旋转 (单旋)</h3><blockquote>
<p><strong>LL left left G的left 的 left 中添加元素所以失衡</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-080907.png" alt=""></p>
<h3 id="RR-左旋转-单旋"><a href="#RR-左旋转-单旋" class="headerlink" title="RR - 左旋转 (单旋)"></a>RR - 左旋转 (单旋)</h3><blockquote>
<p><strong>RR Right Right G的Right 的 Right 中添加元素发生了失衡</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-081423.png" alt=""></p>
<h3 id="LR-RR左旋转-LL右旋转-双旋"><a href="#LR-RR左旋转-LL右旋转-双旋" class="headerlink" title="LR - RR左旋转, LL右旋转 (双旋)"></a>LR - RR左旋转, LL右旋转 (双旋)</h3><blockquote>
<p><strong>LR Left Right G的Left 的 Right 中添加元素发生了失衡,先左旋转,再右旋转</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-082350.png" alt=""></p>
<h3 id="RL-LL右旋转-RR左旋转-双旋"><a href="#RL-LL右旋转-RR左旋转-双旋" class="headerlink" title="RL - LL右旋转, RR左旋转 (双旋)"></a>RL - LL右旋转, RR左旋转 (双旋)</h3><blockquote>
<p><strong>RL Right Left G的Right 的 Left 中添加元素发生了失衡,先右旋转,再左旋转</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-082802.png" alt=""></p>
<p><strong>利用高度判断节点是否失衡 - 左子树的高度 - 右子树的高度</strong></p>
<h3 id="添加-平衡调整"><a href="#添加-平衡调整" class="headerlink" title="添加-平衡调整"></a>添加-平衡调整</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	伪代码 获取平衡因子</span></span><br><span class="line"><span class="comment">//	查验平衡</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">makeBalance</span><span class="params">(Node &lt;E&gt; node)</span></span>&#123;</span><br><span class="line">	<span class="keyword">while</span>((node = node.parent) != <span class="keyword">null</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(isBalancefactor(node))&#123;</span><br><span class="line">      <span class="comment">//	更新高度</span></span><br><span class="line">      updateHeight(node);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">      <span class="comment">//	恢复平衡 这里的node其实是图上的G</span></span><br><span class="line">      rebalance(node);</span><br><span class="line">      <span class="keyword">break</span>;		<span class="comment">//	一旦找到不平衡的直接break</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	获取平衡因子</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">balancefactor</span><span class="params">(Node&lt;&gt; node)</span></span>&#123;</span><br><span class="line"> 	<span class="keyword">int</span> letHeight = node.left == <span class="keyword">null</span> ? <span class="number">0</span> : node.left.height;</span><br><span class="line"> 	<span class="keyword">int</span> rightHeight = node.right == <span class="keyword">null</span> ? <span class="number">0</span> : node.right.height;</span><br><span class="line"> 	<span class="keyword">return</span> leftHeight - rightHeight</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	是否 失去平衡</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isBalancefactor</span><span class="params">(Node&lt;&gt; node)</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> Math.abs(node.balancefactor()) &lt;= <span class="number">1</span>;		<span class="comment">// 小于等于1意味着是平衡因子是 平衡的</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	更新高度 左右子树高度的最大值 加上 1 ,因为是新添加了一个叶子节点么</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">updateHeight</span><span class="params">(Node&lt;E&gt; node)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> letHeight = node.left == <span class="keyword">null</span> ? <span class="number">0</span> : node.left.height;</span><br><span class="line"> 	<span class="keyword">int</span> rightHeight = node.right == <span class="keyword">null</span> ? <span class="number">0</span> : node.right.height;</span><br><span class="line">  node.height = <span class="number">1</span> + (leftHeight &gt; rightHeight ? leftHeight : rightHeight);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	恢复平衡 这里传过来就就图上的G, 高度最低的不平衡的节点</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rebalance</span><span class="params">(Node&lt;E&gt; grand)</span></span>&#123;</span><br><span class="line">  <span class="comment">//	现在g已经有了,要求 p 和 n 图上的!!!</span></span><br><span class="line">  <span class="comment">//	图中发现 p 其实是 g的左右子树高度最高的节点,n是p的左右子树高度最高的节点</span></span><br><span class="line">  	Node&lt;E&gt; parent = grand.tallerChild();		<span class="comment">//	P  tallerChild 返回最高节点方法</span></span><br><span class="line">    Node&lt;E&gt; node = parent.tallerChild();		<span class="comment">//	N</span></span><br><span class="line">  </span><br><span class="line">  <span class="keyword">if</span>(parent.isLeftChild())&#123;		<span class="comment">// L</span></span><br><span class="line">    <span class="keyword">if</span> (node.isLeftChild)&#123;		<span class="comment">// LL</span></span><br><span class="line">				rotateRight(grand);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;										<span class="comment">// LR</span></span><br><span class="line">      rotateLeft(parent);</span><br><span class="line">      rotateRight(grand);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;											<span class="comment">//	R</span></span><br><span class="line">    <span class="keyword">if</span> (node.isLeftChild)&#123;		<span class="comment">// RL</span></span><br><span class="line">      rotateRight(parent);</span><br><span class="line">      rotateLeft(grand);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;										<span class="comment">// RR</span></span><br><span class="line">      rotateLeft(grand);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	左旋 - 看上面的图学就可以了RR </span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotateLeft</span><span class="params">(Node&lt;E&gt; g)</span></span>&#123;</span><br><span class="line">	Node parent = g.right;</span><br><span class="line">  Node t1 = parent.left;</span><br><span class="line">  g.right = t1;</span><br><span class="line">  parent.left = g;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	注意!!! T0到T3都有可能空</span></span><br><span class="line">	<span class="keyword">if</span> (t1 != <span class="keyword">null</span>)&#123;</span><br><span class="line">    	t1.parent = g;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">	g.parent = parent;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	父节点调整</span></span><br><span class="line">  parent.parent = g.parent;</span><br><span class="line">  <span class="keyword">if</span> (g.isLeftChild())&#123;</span><br><span class="line">    g.parent.left = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (g.isRightChild())&#123;</span><br><span class="line">    g.parent.right = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="comment">//	意味着 g是根节点</span></span><br><span class="line">    root = parent;</span><br><span class="line">	&#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	更新高度 先更新G的高度 再更新P的高度 因为G比较矮</span></span><br><span class="line">  updateHeight(g);</span><br><span class="line">  updateHeight(parent);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//	右旋 LL </span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotateRight</span><span class="params">(Node&lt;E&gt; g)</span></span>&#123;</span><br><span class="line">  Node parent = g.lelft;</span><br><span class="line">  Node t2 = parent.right</span><br><span class="line">  g.left = t2;</span><br><span class="line">  parent.right = g;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	注意!!! T0到T3都有可能空</span></span><br><span class="line">	<span class="keyword">if</span> (t2 != <span class="keyword">null</span>)&#123;</span><br><span class="line">    	t2.parent = g;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  g.parent = parent;</span><br><span class="line">  <span class="comment">//	父节点调整</span></span><br><span class="line">  parent.parent = g.parent;</span><br><span class="line">  <span class="keyword">if</span> (g.isLeftChild())&#123;</span><br><span class="line">    g.parent.left = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (g.isRightChild())&#123;</span><br><span class="line">    g.parent.right = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="comment">//	意味着 g是根节点</span></span><br><span class="line">    root = parent;</span><br><span class="line">	&#125;</span><br><span class="line">  </span><br><span class="line">   <span class="comment">//	更新高度 先更新G的高度 再更新P的高度 因为G比较矮</span></span><br><span class="line">  updateHeight(g);</span><br><span class="line">  updateHeight(parent);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	返回最高节点方法</span></span><br><span class="line"><span class="function">Node&lt;e&gt; <span class="title">tallerChild</span><span class="params">(Node&lt;E&gt; node)</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> letHeight = node.left == <span class="keyword">null</span> ? <span class="number">0</span> : node.left.height;</span><br><span class="line"> 	<span class="keyword">int</span> rightHeight = node.right == <span class="keyword">null</span> ? <span class="number">0</span> : node.right.height;</span><br><span class="line">	<span class="keyword">if</span> (leftHeight &gt; rightHeight) <span class="keyword">return</span> node.left;</span><br><span class="line">	<span class="keyword">if</span> (leftHeight &lt; rightHeight) <span class="keyword">return</span> node.right;</span><br><span class="line">  <span class="comment">//	相等</span></span><br><span class="line">  <span class="keyword">return</span> node.isLeftChild() ? node.left : node.right;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	自己是父节点的左节点</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isLeftChild</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> parent != <span class="keyword">null</span> &amp;&amp; <span class="keyword">this</span> == parent.left;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	自己是父节点的右节点</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isRightChild</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> parent != <span class="keyword">null</span> &amp;&amp; <span class="keyword">this</span> == parent.right;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="统一所有旋转操作"><a href="#统一所有旋转操作" class="headerlink" title="统一所有旋转操作"></a>统一所有旋转操作</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-15-025017.png" alt=""></p>
<blockquote>
<p><strong>从上面的图中发现,不管是LL,还是RR,LR,RL,最终旋转后他们变成了一样</strong></p>
<p><strong>a b c 是自组, d是root,e f g是一组</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotate</span> <span class="params">(Node&lt;E&gt; root 之前的跟节点,</span></span></span><br><span class="line"><span class="function"><span class="params">						 Node&lt;E&gt; a,Node&lt;E&gt; b, Node&lt;E&gt; c,</span></span></span><br><span class="line"><span class="function"><span class="params">						 Node&lt;E&gt; e,Node&lt;E&gt; f, Node&lt;E&gt; g,)</span></span>&#123;</span><br><span class="line">  <span class="comment">//	让d成为这棵树子树的根节点</span></span><br><span class="line">	d.parent = r.parent;</span><br><span class="line">  <span class="keyword">if</span> (r.isLeftChild(),意思r是它跟节点的左边)&#123;</span><br><span class="line">    r.parent.left = d;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (r.isRightChild(),意思r是它跟节点的右边)&#123;</span><br><span class="line">    r.parent.right = d;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    root = d;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	a - b - c</span></span><br><span class="line">  b.left = a;</span><br><span class="line">  <span class="keyword">if</span> (a != <span class="keyword">null</span>)&#123;</span><br><span class="line">    a.parent = b;</span><br><span class="line">  &#125;</span><br><span class="line">   b.right = c;</span><br><span class="line">  <span class="keyword">if</span> (c != <span class="keyword">null</span>)&#123;</span><br><span class="line">    c.parent = b;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//	更新b的高度</span></span><br><span class="line">  updateHeight(b);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	e - f - g</span></span><br><span class="line">  f.left = e;</span><br><span class="line">  <span class="keyword">if</span> (a != <span class="keyword">null</span>)&#123;</span><br><span class="line">    e.parent = f;</span><br><span class="line">  &#125;</span><br><span class="line">   f.right = g;</span><br><span class="line">  <span class="keyword">if</span> (g != <span class="keyword">null</span>)&#123;</span><br><span class="line">    g.parent = f;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//	更新f的高度</span></span><br><span class="line">  updateHeight(f);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	b - d - f</span></span><br><span class="line">  d.left = b;</span><br><span class="line">  d.right = f;</span><br><span class="line">  </span><br><span class="line">  b.parent = d;</span><br><span class="line">  f.parent = d;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	更新d的高度</span></span><br><span class="line">  updateHeight(d);</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="删除-调整平衡"><a href="#删除-调整平衡" class="headerlink" title="删除-调整平衡"></a>删除-调整平衡</h3><blockquote>
<p><strong>删除16,15就会失去平衡,删除子节点 只可能会导致父节点失衡,其他节点不可能失衡</strong></p>
<p><strong>删除导致失衡,说明删除的那个节点绝对是相对短的节点,所以不会影响父节点的高度,所以上面的祖先节点都不会有变化</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-015707.png" alt=""></p>
<p><strong>LL - 右旋转 (单旋)</strong></p>
<blockquote>
<p><strong>红色是删除的节点,绿色有可能不存在的节点</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024052.png" alt=""></p>
<blockquote>
<p><strong>删除之后G失去平衡</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024239.png" alt=""></p>
<blockquote>
<p><strong>调整右旋转之后</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024410.png" alt=""></p>
<blockquote>
<p><strong>G恢复了平衡,但是由于T0的高度减少了,那么P的高都是T2,那么如果绿色的部分不存在,那么P的高度比之前G的高度是-1的,所以P的祖先节点也有可能失衡,所以不断的查询祖先节点是否失衡,所以删除while循环的Break,复杂度O(longn),最多调整logn次</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	伪代码 获取平衡因子</span></span><br><span class="line"><span class="comment">//	查验平衡   删除跟添加一样 不同的地方时break删掉就可以了</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">makeBalance</span><span class="params">(Node &lt;E&gt; node)</span></span>&#123;</span><br><span class="line">	<span class="keyword">while</span>((node = node.parent) != <span class="keyword">null</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(isBalancefactor(node))&#123;</span><br><span class="line">      <span class="comment">//	更新高度</span></span><br><span class="line">      updateHeight(node);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">      <span class="comment">//	恢复平衡 这里的node其实是图上的G</span></span><br><span class="line">      rebalance(node);</span><br><span class="line">      <span class="comment">//break;		//	一旦找到不平衡的直接break</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><blockquote>
<p><strong>添加</strong></p>
<p><strong>可能会导致 所有 祖先节点 都失衡</strong></p>
<p><strong>但是只要让高度最低的失衡节点回复平衡,整棵树就恢复平衡(仅需O(1)次调整)</strong></p>
<p><strong>删除</strong></p>
<p><strong>只可能会导致父节点失衡</strong></p>
<p><strong>但是让父节点恢复平衡后,可能会导致更高层的祖先节点失衡,(最多需要O(logn)次调整)</strong></p>
<p><strong>平均时间复杂度</strong></p>
<p><strong>搜索:    O(logn)</strong></p>
<p><strong>添加:    O(logn),    仅需O(1)次的旋转操作</strong></p>
<p><strong>删除:    O(logn),    最多需要O(logn)次的旋转操作</strong>    </p>
</blockquote>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/12/SearchBinaryTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/12/SearchBinaryTree/" itemprop="url">BinarySearchTree</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-12T14:09:47+08:00">
                2019-10-12
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/12/SearchBinaryTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/12/SearchBinaryTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="二叉搜索树-BinarySearchTree"><a href="#二叉搜索树-BinarySearchTree" class="headerlink" title="二叉搜索树 BinarySearchTree"></a>二叉搜索树 BinarySearchTree</h3><blockquote>
<p><strong>任意一个节点值都 大于 其左子树所有节点的值</strong></p>
<p><strong>任意一个节点的值都小于其右子树所有节点的值</strong></p>
<p><strong>它的左右子树也是一棵二叉搜索树</strong></p>
<p><strong>存数的元素必须具备比较性</strong></p>
<p><strong>不能为空</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-061916.png" alt=""></p>
<h3 id="二叉搜索树-添加"><a href="#二叉搜索树-添加" class="headerlink" title="二叉搜索树-添加"></a>二叉搜索树-添加</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	伪代码</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span> <span class="params">(E element)</span></span>&#123;</span><br><span class="line">	<span class="comment">//	判断element是否为空</span></span><br><span class="line">	<span class="keyword">if</span> (root == <span class="keyword">null</span>)&#123;</span><br><span class="line">		root = <span class="keyword">new</span> Node&lt;&gt;(element,父节点传<span class="keyword">null</span>);</span><br><span class="line">		size++;	<span class="comment">//	大小++ </span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//	到这里不是第一个节点,顺着根节点往下找添加合适的位置</span></span><br><span class="line">	Node&lt;E&gt; node = root;</span><br><span class="line">	<span class="comment">//	父节点</span></span><br><span class="line">	Node&lt;E&gt; parent = root;</span><br><span class="line">	<span class="comment">//	比较结果</span></span><br><span class="line">	<span class="keyword">int</span> cmp = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(node != <span class="keyword">null</span>)&#123;</span><br><span class="line">		cmp = compare(element,node.element);</span><br><span class="line">		<span class="comment">//	在这个位置保存父节点</span></span><br><span class="line">		parent = node;</span><br><span class="line">		<span class="keyword">if</span> (cmp &gt; <span class="number">0</span>)&#123;					<span class="comment">//	如果存储的值大于 node,那么把node换成node.right	一直比较,直到right == null为止,那么在空的位置添加element</span></span><br><span class="line">			node = node.right</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>)&#123;</span><br><span class="line">			node = node.left		<span class="comment">//	如果存储的值小于 node,那么把node换成node.left	一直比较,直到left == null为止,那么在空的位置添加element</span></span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="comment">//	覆盖原先的值</span></span><br><span class="line">				node.element = element;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//	循环结束之后 利用cmp判断在左还是右</span></span><br><span class="line">	<span class="keyword">if</span> (cmp &gt; <span class="number">0</span>)&#123;</span><br><span class="line">			parent.right = Node&lt;E&gt;(element,parent);	<span class="comment">//保存</span></span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			parent.left = Node&lt;E&gt;(element,parent);	<span class="comment">//保存</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	比较大小 返回 大于0 e1 &gt; e2  小于0 e1 &lt; e2 等于=0  e1=e2 </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">compare</span><span class="params">(e1, e2)</span></span>&#123;<span class="keyword">return</span> el - e2;&#125;</span><br></pre></td></tr></table></figure>
<h3 id="二叉搜索树-删除"><a href="#二叉搜索树-删除" class="headerlink" title="二叉搜索树-删除"></a>二叉搜索树-删除</h3><blockquote>
<p><strong>删除叶子节点,直接删除 node.parent.left or right = null, if node.parent == null, do root = null</strong></p>
</blockquote>
<blockquote>
<p><strong>删除度为1的跟节点root = child , child.parent = null</strong></p>
</blockquote>
<blockquote>
<p><strong>删除度为1的节点node,子节点是node.left或者子节点是node.right</strong></p>
<p><strong>用子节点替代node的位置</strong></p>
<p><strong>子节点的parent属性维护和parent的left或者right</strong></p>
</blockquote>
<blockquote>
<p><strong>删除度为2的节点node, 用node的前驱节点或者后继节点替换node</strong></p>
</blockquote>
<h3 id="前序遍历-Preorder-Traversal"><a href="#前序遍历-Preorder-Traversal" class="headerlink" title="前序遍历 (Preorder Traversal)"></a>前序遍历 (Preorder Traversal)</h3><blockquote>
<p><strong>访问顺序 根节点 , 前序遍历左子树,前序遍历右子树</strong></p>
<p><code>7</code>   , 4 , 2 , 1 , 3 , 5 ,        9 , 8 , 11 , 10 , 12</p>
</blockquote>
<h3 id=""><a href="#" class="headerlink" title=""></a><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-063004.png" alt=""></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)perorderTraversal:(Node *)node&#123;</span><br><span class="line">		if (node == nil) return;</span><br><span class="line">		NSLog(@&quot;%@&quot;,node.element);</span><br><span class="line">	  [self perorderTraversal:nodel.left];</span><br><span class="line">	  [self perorderTraversal:nodel.right];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="中序遍历-Inorder-Traversal"><a href="#中序遍历-Inorder-Traversal" class="headerlink" title="中序遍历 Inorder Traversal"></a>中序遍历 Inorder Traversal</h3><blockquote>
<p><strong>访问顺序 中序遍历左子树   根节点  中序遍历右子树</strong></p>
<p><strong>1  2  3  4  5    <code>7</code>     8  9  10  11  12  规律如果中序遍历的这个树是二叉搜索树,遍历出来的结果是升序,那么反过来就是降序</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-065007.png" alt=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)inorderTraversal:(Node *)node&#123;</span><br><span class="line">		if (node == nil) return;</span><br><span class="line">	  [self inorderTraversal:nodel.left];</span><br><span class="line">		NSLog(@&quot;%@&quot;,node.element);</span><br><span class="line">	  [self inorderTraversal:nodel.right];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="后序遍历-Post-order-Traversal"><a href="#后序遍历-Post-order-Traversal" class="headerlink" title="后序遍历 Post order Traversal"></a>后序遍历 Post order Traversal</h3><blockquote>
<p><strong>访问顺序 后序遍历左子树   后序遍历右子树  根节点,先访问左还是右 取决于自己,后序遍历的意思是根节点放在最后面就可以了</strong></p>
<p>1  3  2  5  4    8  10  12  11  9       <code>7</code></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-070038.png" alt=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)postorderTraversal:(Node *)node&#123;</span><br><span class="line">		if (node == nil) return;</span><br><span class="line">	  [self postorderTraversal:nodel.left];</span><br><span class="line">	  [self postorderTraversal:nodel.right];</span><br><span class="line">		NSLog(@&quot;%@&quot;,node.element);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="层序遍历-Level-Order-Traversal"><a href="#层序遍历-Level-Order-Traversal" class="headerlink" title="层序遍历 Level Order Traversal"></a>层序遍历 Level Order Traversal</h3><blockquote>
<p><strong>访问顺序 从上到下 从左到右</strong></p>
<p><code>7</code>   4  9  2  5  8  11  1  3  10  12</p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-070445.png" alt=""></p>
<blockquote>
<p><strong>实现思路:  使用队列 先进先出FIFO</strong></p>
<p><strong>1. 将根节点入队</strong></p>
<p><strong>2.循环执行以下操作,直到队列为空</strong></p>
<p><strong>将队列的第一个元素出队,进行访问</strong></p>
<p><strong>将出队的左子节点入队</strong></p>
<p><strong>将出队的右子树入队</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)levelOrderTraversal&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span>;</span><br><span class="line">	  Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line">	  	System.out.println(node.element);</span><br><span class="line">	  	<span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  		queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">	  	<span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  		queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">	  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="计算二叉树的高度"><a href="#计算二叉树的高度" class="headerlink" title="计算二叉树的高度"></a>计算二叉树的高度</h3><p><strong>递归</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	查询子节点最高的节点的高度 + 1 就是字节的高度</span></span><br><span class="line">- (<span class="keyword">void</span>)height:(Node *)node&#123;</span><br><span class="line">	<span class="keyword">if</span> (node == nill) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">1</span> + max(height(node.left),height(node.right));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>层序遍历</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)levelOrderTraversal&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span>;</span><br><span class="line">  	<span class="comment">//	树的高度</span></span><br><span class="line">		<span class="keyword">int</span> height = <span class="number">0</span>;	</span><br><span class="line">  	<span class="comment">//	在队列中存储的叶子总数</span></span><br><span class="line">  	<span class="keyword">int</span> levelSize = <span class="number">1</span>;	<span class="comment">//	根节点入队默认有一个</span></span><br><span class="line">  </span><br><span class="line">	  Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">  </span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">      </span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line">      levelSize--;</span><br><span class="line">	  	System.out.println(node.element);</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      <span class="keyword">if</span> (levelSize == <span class="number">0</span>)&#123;</span><br><span class="line">        <span class="comment">//	levelSize == 0以为这这一层遍历完,要访问下一层</span></span><br><span class="line">        	levelSize = queue.size;</span><br><span class="line">					height++;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">	  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="判断一棵树是否为完全二叉树"><a href="#判断一棵树是否为完全二叉树" class="headerlink" title="判断一棵树是否为完全二叉树"></a>判断一棵树是否为完全二叉树</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-021056.png" alt=""></p>
<p><strong>层序遍历判断</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">boolean</span>)isComplete:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == <span class="keyword">null</span>) <span class="keyword">return</span>;</span><br><span class="line">		</span><br><span class="line">		Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">  	<span class="keyword">boolean</span> leaf = <span class="keyword">false</span>;					<span class="comment">//	是否是叶子节点</span></span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (leaf &amp;&amp; (node.left || node.right))&#123;</span><br><span class="line">						<span class="keyword">return</span> <span class="keyword">false</span>;		<span class="comment">//	要求节点是叶子节点,但是不是那么不是完全二叉树</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (root.left != <span class="keyword">null</span> &amp;&amp; root.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">            queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">              queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">         		&#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (root.left == <span class="keyword">null</span> &amp;&amp; root.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">          <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;		<span class="comment">//	后面遍历的节点都必须是叶子节点</span></span><br><span class="line">					leaf = <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> 	<span class="keyword">return</span> ture;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="翻转二叉树"><a href="#翻转二叉树" class="headerlink" title="翻转二叉树"></a>翻转二叉树</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-011012.png" alt=""></p>
<p><strong>利用前序遍历翻转</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)perorderTraversal:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span> root;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//	每当遍历一个节点交换一个节点</span></span><br><span class="line">			Node *tmp = root.left;</span><br><span class="line">			root.left = root.right;</span><br><span class="line">			root.right = root.tmp;</span><br><span class="line">			<span class="comment">//	这里交换之后遍历不会影响,只是先遍历左还是右</span></span><br><span class="line">	  	[self perorderTraversal:root.left];</span><br><span class="line">	  	[self perorderTraversal:root.right];</span><br><span class="line">  <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>利用后序遍历翻转</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)perorderTraversal:(Node *)root&#123;</span><br><span class="line">		if (root == nil) return root;</span><br><span class="line"></span><br><span class="line">			//	这里交换之后遍历不会影响,只是先遍历左还是右</span><br><span class="line">	  	[self perorderTraversal:root.left];</span><br><span class="line">	  	[self perorderTraversal:root.right];</span><br><span class="line"></span><br><span class="line">			//	每当遍历一个节点交换一个节点</span><br><span class="line">			Node *tmp = root.left;</span><br><span class="line">			root.left = root.right;</span><br><span class="line">			root.right = root.tmp;</span><br><span class="line">			</span><br><span class="line">  return root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>利用中序遍历翻转</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)inorderTraversal:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span> root;</span><br><span class="line"></span><br><span class="line">	  	[self inorderTraversal:root.left];</span><br><span class="line">	  	</span><br><span class="line">			<span class="comment">//	每当遍历一个节点交换一个节点</span></span><br><span class="line">			Node *tmp = root.left;</span><br><span class="line">			root.left = root.right;</span><br><span class="line">			root.right = root.tmp;</span><br><span class="line">			</span><br><span class="line">	  	[self inorderTraversal:root.left];</span><br><span class="line">	  	</span><br><span class="line">	  	</span><br><span class="line">  <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>层序遍历翻转</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)levelOrderTraversal:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span>;  </span><br><span class="line">	  Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">  </span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">      </span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line">	  	System.out.println(node.element);</span><br><span class="line">      </span><br><span class="line">      <span class="comment">//	交换</span></span><br><span class="line">      Node *tmp = node.left;</span><br><span class="line">			node.left = node.right;</span><br><span class="line">			node.right = node.tmp;</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">	  &#125;</span><br><span class="line">  <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="根据遍历结果还原二叉树"><a href="#根据遍历结果还原二叉树" class="headerlink" title="根据遍历结果还原二叉树"></a>根据遍历结果还原二叉树</h3><p><strong>以下结果可以保证还原出唯一的一棵二叉树</strong></p>
<p><strong>前序遍历 + 中序遍历</strong></p>
<p><strong>后序遍历 + 中序遍历</strong></p>
<p><strong>如果 只是给定前序遍历和后序遍历 那么必须是真二叉树才能还原出唯一的一棵二叉树</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025001.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025022.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025405.png" alt=""></p>
<h3 id="前驱节点"><a href="#前驱节点" class="headerlink" title="前驱节点"></a>前驱节点</h3><blockquote>
<p><strong>中序遍历时的前一个节点</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-030908.png" alt=""></p>
<p><strong>8的前驱节点 是7,4的前驱节点是4,也就是node.left.right.right.right 直到 right == nil为止,度为1或者0</strong></p>
<p><strong>如果left == nil 那么一直往上找父节点,9的父节点是10(但9不是10的右节点),再往上找13(但10不是13的右节点),再往上找(13是8的右节点)8,那么8就是9的前驱节点</strong></p>
<p><strong>node.left == nil &amp;&amp; node.parent == nill 没有前驱节点</strong></p>
<h3 id="后继节点"><a href="#后继节点" class="headerlink" title="后继节点"></a>后继节点</h3><blockquote>
<p><strong>中序遍历后一个节点</strong></p>
</blockquote>
<p><strong>node.right.left.left.left 直到 left == nil 为止,度为1或者0</strong></p>
<h4 id="二叉搜索树删除"><a href="#二叉搜索树删除" class="headerlink" title="二叉搜索树删除"></a>二叉搜索树删除</h4><p><strong>比如要删除8,那么用8的前驱节点或者后继节点,替换掉8的值(不是删除8),再删除7</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	接口</span></span><br><span class="line">- (<span class="keyword">void</span>)removeElement:(E element)&#123;</span><br><span class="line">  	<span class="keyword">if</span> (element == <span class="keyword">null</span>) <span class="keyword">return</span>;</span><br><span class="line">  	removeNode(getNode(element));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	删除</span></span><br><span class="line">- (<span class="keyword">void</span>)removeNode:(Node *node)&#123;</span><br><span class="line">		size--;</span><br><span class="line">  	<span class="keyword">if</span> (node的度为<span class="number">2l</span>eft和right不为空)&#123;</span><br><span class="line">      Node *s = successor(node)	<span class="comment">//	找到后继节点</span></span><br><span class="line">      node.element = s.element	<span class="comment">//	替换值</span></span><br><span class="line">			node =  s;	???</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  <span class="comment">//	度为1的节点,走到这里必然是度为1的节点</span></span><br><span class="line">		Node *replacement = node.left != <span class="keyword">null</span> ? node.left : node.right;</span><br><span class="line">  	<span class="keyword">if</span> (replacement != <span class="keyword">null</span>)&#123;</span><br><span class="line">      	replacement.parent = node.parent;</span><br><span class="line">      <span class="comment">//	更改parent的left和right的值</span></span><br><span class="line">      <span class="keyword">if</span> (node.parent == <span class="keyword">null</span>)&#123;</span><br><span class="line">       root = replacement;  </span><br><span class="line">      &#125;<span class="keyword">else</span> <span class="keyword">if</span> (node == ndoe.parent.left)&#123;</span><br><span class="line">        node.parent.left = replacement;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (node == ndoe.parent.right)&#123;</span><br><span class="line">        node.parent.right = replacement;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (node.parent == <span class="keyword">null</span>)&#123;	<span class="comment">//	node是叶子节点并且是根节点</span></span><br><span class="line">      root == <span class="keyword">null</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span>&#123;														<span class="comment">//	叶子节点</span></span><br><span class="line">      <span class="keyword">if</span> (node == node.parent.right)&#123;</span><br><span class="line">        node.parent.right == <span class="keyword">null</span>;</span><br><span class="line">      &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        node.parent.left == <span class="keyword">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">		&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	拿到Node</span></span><br><span class="line">- (Node *)getNode:(E element)&#123;</span><br><span class="line">		Node *node = root;</span><br><span class="line">		<span class="keyword">while</span> (node != <span class="keyword">null</span>)&#123;</span><br><span class="line">			<span class="keyword">if</span> (node.element == element) <span class="keyword">return</span> node;</span><br><span class="line">			<span class="keyword">if</span> (node.element &lt; element)&#123;</span><br><span class="line">				node = node.right;</span><br><span class="line">			&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">				node = node.left;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="二叉搜索树复杂度分析"><a href="#二叉搜索树复杂度分析" class="headerlink" title="二叉搜索树复杂度分析"></a>二叉搜索树复杂度分析</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-065312.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-065720.png" alt=""></p>
<h3 id="改进二叉搜索树"><a href="#改进二叉搜索树" class="headerlink" title="改进二叉搜索树"></a>改进二叉搜索树</h3><p><strong>改进: 在节点的添加,删除操作之后,想办法让二叉搜索树恢复平衡(减少树的高度),左右子树的高度尽量接近</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-070327.png" alt=""></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/11/BinaryTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/11/BinaryTree/" itemprop="url">BinaryTree</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-11T16:36:33+08:00">
                2019-10-11
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/11/BinaryTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/11/BinaryTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="二叉树-BinaryTree"><a href="#二叉树-BinaryTree" class="headerlink" title="二叉树 BinaryTree"></a>二叉树 BinaryTree</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-012223.png" alt=""></p>
<p><strong>1为根节点, 1是 2,3,4,5,6的父节点,相反他们是1的子节点,2,3,4,5,6是兄弟节点(父节点是同一个,并且在同一个层侧级别上)</strong></p>
<p><strong>一个树可以没有人和街店,成为空树</strong></p>
<p><strong>节点的度(degree): 1的度为5,因为他有5颗子树,2的度为2,61的度为0</strong></p>
<p><strong>树的度:           所有节点度中最大值,这棵树树的度为5</strong></p>
<p><strong>叶子节点:       度为0的节点,比如 4 21 221 222 223 31 51 52 61</strong></p>
<p><strong>非叶子节点:   度不是为0的节点为非叶子节点比如 2 3 5 6 22 </strong></p>
<p><strong>层数:              上面这棵树的层数为 4层,层数有些人是从0开始数</strong></p>
<p><strong>节点的深度(depth):         从根节点到当前节点的唯一路径上的节点总数,2的节点深度为2(经过了1和2),31的深度为3经过了1 3 31</strong></p>
<p><strong>节点的高度:       从当前节点到最远叶子节点的路径上的节点总数,2的节点高度为3</strong></p>
<p><strong>树的深度:          所有节点深度中的最大值,上面这棵树的深度为4</strong></p>
<p><strong>树的高度:          所有节点高度中的最大值 上面这棵树的高度为4</strong></p>
<p><strong>树的 深度 等于 树的 高度</strong></p>
<h3 id="有序树"><a href="#有序树" class="headerlink" title="有序树"></a>有序树</h3><p><strong>树中任意节点的子节点之间有顺序关系: 比如上面这棵树的第二层(仅第二层)</strong></p>
<h3 id="无序树"><a href="#无序树" class="headerlink" title="无序树"></a>无序树</h3><blockquote>
<p> <strong>树中任意节点的节点之间没有顺序关系,也称为自由树</strong></p>
</blockquote>
<h3 id="森林"><a href="#森林" class="headerlink" title="森林"></a>森林</h3><blockquote>
<p><strong>有m (m &gt;= 0) 课互不相交的树组成的集合: 也就是很多树接在一起层位森林</strong></p>
</blockquote>
<h3 id="二叉树Binary-Tree"><a href="#二叉树Binary-Tree" class="headerlink" title="二叉树Binary Tree"></a>二叉树Binary Tree</h3><blockquote>
<p><strong>每个节点的度最大为2(最多拥有 2 棵子树)</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-014949.png" alt=""></p>
<p><strong>即使某节点只有一棵树,也要区分左右子树,不能调换,所以有序树</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-015516.png" alt=""></p>
<p><strong>非空二叉树的第i层,最多有(2的 i-1次方)个节点 i &gt;= 1</strong></p>
<p><strong>2º = 1  2 的1次方= 2, 2 的平方= 4, 2的3次方 = 8</strong></p>
<p><strong>高度为h的二叉树上最多有 (2的 h次方-1) 个节点 (h &gt;= 1)</strong></p>
<p><strong>公式:    对于任何一棵非空二叉树,如果叶子节点个数为n0,度为2的节点数为n2,则 n0 = n2 + 1</strong></p>
<h3 id="真二叉树-Proper-Binary-Tree"><a href="#真二叉树-Proper-Binary-Tree" class="headerlink" title="真二叉树 Proper Binary Tree"></a>真二叉树 Proper Binary Tree</h3><hr>
<blockquote>
<p><strong>所有节点的度都要么为0,要么为2</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-021444.png" alt=""></p>
<h3 id="满二叉树-Full-Binary-Tree"><a href="#满二叉树-Full-Binary-Tree" class="headerlink" title="满二叉树 Full Binary Tree"></a>满二叉树 Full Binary Tree</h3><blockquote>
<p> <strong>所有节点的度都要么为0,要么为2,且所有的叶子节点都在最后一层</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-074102.png" alt=""></p>
<p><strong>满二叉树一定是真二叉树,真二叉树不一定是满二叉树</strong></p>
<p><strong>高度: log2(n + 1)</strong></p>
<h3 id="完全二叉树-Complete-Binary-Tree"><a href="#完全二叉树-Complete-Binary-Tree" class="headerlink" title="完全二叉树 Complete Binary Tree"></a>完全二叉树 Complete Binary Tree</h3><blockquote>
<p><strong>叶子节点只会出现在最后2层,且最后一层的叶子节点都靠左对齐,也就是从上到下,从左到右排序,度为1的节点只有左子树,度为1 的节点要么是1个要么是0个</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-022418.png" alt=""></p>
<p><strong>假设完全二叉树的高度为h(h &gt;= 1),那么至少有(2的h-1次方)个节点,最多 (2的h次方 - 1)(满二叉树的形态)</strong></p>
<p><strong>面试题&amp;&amp;公式</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-033718.jpg" alt=""></p>
<p><strong>题: 一个<code>完全!!!</code>二叉树有768个节点,求叶子节点的个数,也就是说 sum = 768 求出n0</strong></p>
<p><strong>总节点个数 n = n0 + n1 + n2 而且 n0 = n2 + 1</strong></p>
<p><strong>那么可以这样写n = n0 + n1 + n0 - 1, n = 2n0 + n1 - 1,从图或完全二叉树的规则上一直n1 要么是0要是1</strong></p>
<p><strong>n1为1时 总结点数n必然是偶数, n1为0时 总结点数必然是奇数</strong></p>
<p><strong>偶数时 会变成这样 n = 2n0,我们求的是n0 那么 n0 = n / 2,也就是 768 / 2那么非叶子节点也是 n/2</strong></p>
<p><strong>奇数时 会变成  n = 2n0 - 1,我们求的是n0 那么n0 = (n + 1) / 2, 非叶子节点 n - (n + 1) / 2变化 n - n / 2 - 1 / 2,在变化 n / 2 - 1 / 2 在变化 (n - 1) / 2 </strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">偶数时 	n0(叶子节点数量) = n / 2</span><br><span class="line">奇数		 n0(叶子节点数量) = (n + 1) / 2</span><br><span class="line">在变成的时候需要判断 n 是偶数还是奇数 需要 n % 2 == 0奇数这样,就觉得很麻烦</span><br><span class="line"></span><br><span class="line">变成一个公式</span><br><span class="line"></span><br><span class="line">假如就 (n + 1) / 2这个公式去算n0,那么奇数的时候是正确的(结果跟定是整数,不是浮点数),但是偶数的时候这个公式多了一个 1 / 2(结果肯定是浮点数),所以下取整数正好是n0,这样的话不管是偶数还是奇数都可以用这个公式</span><br><span class="line">向下取整数floor((n + 1) / 2),而编程中默认就是向下取整所以floor可以省略,就变成(n + 1) / 2</span><br><span class="line">又可以利用位运算变化成 (n + 1) &gt;&gt; 1 右移动1位</span><br><span class="line"></span><br><span class="line">反过来 假如就 n / 2 这个公式去算n0  那么就是向上取整数</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/07/19/动态数组/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/07/19/动态数组/" itemprop="url">动态数组-02</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-07-19T15:23:25+08:00">
                2019-07-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/19/动态数组/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/07/19/动态数组/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/07/17/数据结构与算法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/07/17/数据结构与算法/" itemprop="url">数据结构与算法-01</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-07-17T13:16:51+08:00">
                2019-07-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/17/数据结构与算法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/07/17/数据结构与算法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h3 id="复杂度"><a href="#复杂度" class="headerlink" title="复杂度"></a>复杂度</h3><p><img src="http://img1.imgtn.bdimg.com/it/u=2111815583,2613041922&amp;fm=26&amp;gp=0.jpg" alt="image"></p>
<p><strong>获取斐波那契数:经过测试 <code>fib2</code>的速度很快,而<code>fib1</code>的就很慢</strong></p>
<p><code>算法题练习网站leetcode-cn.com,leetcode.com</code>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2019/07/17/数据结构与算法/#more" rel="contents">
              Read more &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/24/swift5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/24/swift5/" itemprop="url">swift5</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-06-24T09:07:48+08:00">
                2019-06-24
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/24/swift5/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/24/swift5/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h4 id="条件语句"><a href="#条件语句" class="headerlink" title="条件语句"></a>条件语句</h4><p><strong>While</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// MARK: - While &gt;&gt; do while</span></span><br><span class="line"><span class="keyword">let</span> num = -<span class="number">1</span></span><br><span class="line"><span class="keyword">repeat</span> &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">"num is \(num)"</span>)</span><br><span class="line">&#125;<span class="keyword">while</span> num &gt; <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> names = [<span class="string">"Anna"</span>,<span class="string">"Alex"</span>,<span class="string">"Brian"</span>,<span class="string">"jack"</span>]</span><br><span class="line"><span class="keyword">let</span> range = <span class="number">0</span>...<span class="number">3</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">"name is \(names[i])"</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> name <span class="keyword">in</span> names[<span class="number">0</span>...<span class="number">3</span>] &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">"name is \(name)"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2019/06/24/swift5/#more" rel="contents">
              Read more &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/19/Function/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/02/19/Function/" itemprop="url">Function</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-02-19T15:02:47+08:00">
                2019-02-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Function/" itemprop="url" rel="index">
                    <span itemprop="name">Function</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/02/19/Function/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/02/19/Function/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h3 id="srand-unsigned-time-NULL"><a href="#srand-unsigned-time-NULL" class="headerlink" title="srand((unsigned)time(NULL));"></a>srand((unsigned)time(NULL));</h3><p><strong>srand函数是随机数发生器的初始化函数。原型:void srand(unsigned seed);</strong></p>
<p><strong>用法:它初始化随机种子，会提供一个种子，这个种子会对应一个随机数，如果使用相同的种子后面的rand()函数会出现一样的随机数，如: srand(1); 直接使用1来初始化种子。不过为了防止随机数每次重复，常常使用系统时间来初始化，即使用 time函数来获得系统时间，它的返回值为从 00:00:00 GMT, January 1, 1970 到现在所持续的秒数，然后将time_t型数据转化为(unsigned)型再传给srand函数，即: srand((unsigned) time(&amp;t)); 还有一个经常用法，不需要定义time_t型t变量,即: srand((unsigned) time(NULL)); 直接传入一个空指针，因为你的程序中往往并不需要经过参数获得的数据。</strong>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2019/02/19/Function/#more" rel="contents">
              Read more &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/01/08/OCCallSwift/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/01/08/OCCallSwift/" itemprop="url">OCCallSwift</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-01-08T11:33:11+08:00">
                2019-01-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/OC-Swift/" itemprop="url" rel="index">
                    <span itemprop="name">OC-Swift</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/01/08/OCCallSwift/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/01/08/OCCallSwift/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h3 id="OC调用Swift"><a href="#OC调用Swift" class="headerlink" title="OC调用Swift"></a>OC调用Swift</h3><p><strong>1. Swift类必须继承NSObject</strong></p>
<p><strong>2. 自动创建桥接文件时选择创建,但是没有任何东西</strong></p>
<p><strong>3. 选择TARGETS → BuildSettings → Packaging → Defines Module 为 YES</strong></p>
<p><strong>4. 在需要使用Swift的地方导入 #import”工程名-Swift.h”,是因为 Product Module Name 是工程名</strong></p>
<p><strong>5. 有时候初始化方法找不到可以试试<code>@objc</code>或者更换Swift版本,如下方的图</strong></p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2019/01/08/OCCallSwift/#more" rel="contents">
              Read more &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/12/20/UIPickerView/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/12/20/UIPickerView/" itemprop="url">UIPickerView自定义日期选择</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-12-20T08:40:00+08:00">
                2018-12-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/UIPickerView/" itemprop="url" rel="index">
                    <span itemprop="name">UIPickerView</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/12/20/UIPickerView/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/12/20/UIPickerView/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h3 id="UIPickerView"><a href="#UIPickerView" class="headerlink" title="UIPickerView"></a>UIPickerView
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2018/12/20/UIPickerView/#more" rel="contents">
              Read more &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
